package com.ruoyi.system.ali;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.utils.file.AliOss;
import com.ruoyi.common.utils.sign.SignMD5Util;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.UserRate;
import com.ruoyi.system.dto.ApiByPhoneAddDto;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.ApiPayResultVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.TreeMap;

/**
 * 当面付
 */
@Component
public class XhbPayService implements ChannelService {
    @Resource
    ISysConfigService configService;
    private static final String serverUrl = "https://openapi.alipay.com/gateway.do";
    @Resource
    private ZfbConfigService zfbConfigService;
    @Resource
    private BankService bankService;
    @Resource
    private ISysUserService userService;
    @Resource
    private UserRateService userRateService;

    @Override
    public String getType() {
        //return ChannelEnum.ZFB_XHB.getCode();
        return null;
    }

    @Resource
    private CashBizService cashService;

    @Override
    public Object create(AliPayDto aliPayDto, Order order) {
        Bank bank = bankService.getRand(order.getAmount(), getType());
        return create(order, bank);
    }

    @Override
    public Object createByPhone(ApiByPhoneAddDto apiByPhoneAddDto, Order order) {
        Bank bank = bankService.getUserBank(order.getUserId(), order.getAmount(), getType());
        return create(order, bank);
    }

    //uid
    public Object create(Order order, Bank bank) {

        //获取一个支付宝的uid
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("s", "money");
        jsonObject.put("u", bank.getZfbUserId());
//        jsonObject.put("u", "2323232323");
        jsonObject.put("a", order.getAmount());
        jsonObject.put("m", "温馨提示: 为减少风控 ,请尽量停顿30秒以上付款");
//        String decode = URLDecoder.decode(jsonObject.toJSONString(), Charset.defaultCharset());
        //将链接解析成二维码
        String qrCode = bank.getZfbUrl();
        QrConfig qrConfig = new QrConfig();
        qrConfig.setWidth(200);
        qrConfig.setHeight(200);
        byte[] bytes = QrCodeUtil.generatePng(qrCode, qrConfig);
        String projectName = configService.selectConfigByKey("projectName");
        String picUrl = AliOss.uploadToOss(bytes, projectName + "/orderReceive/" + order.getOrderNo() + ".jpg");
//        Map<String, Object> map = new HashMap<>();
//        //移动端支付宝跳转链接
//        map.put("url", qrCode);
//        map.put("pic", picUrl);
//        map.put("amount", order.getAmount());
//        map.put("orderNo", order.getOrderNo());
        AliPayVo aliPayVo = new AliPayVo();
        aliPayVo.setOrderNo(order.getOrderNo());
        aliPayVo.setAmount(order.getAmount());
        aliPayVo.setPic(picUrl);
        aliPayVo.setQrCode(qrCode);
        //冻结收款用户的金额
        CashHistory userCashHis = new CashHistory(bank.getUserId(), order.getAmount(), "reduce", "代收订单", "freeze");
        userCashHis.setOrderNo(order.getOrderNo());
        cashService.updateCash(bank.getUserId(), userCashHis);
        SysUser sysUser = userService.check(bank.getUserId());
        UserRate userRate = userRateService.checkUserRate(bank.getUserId(), this.getType());

        order.setEarning(order.getAmount().multiply(sysUser.getZfbRate().divide(new BigDecimal(100))));
        order.setAccount(bank.getCardNo());
        order.setName(bank.getName());
        order.setChannelName(ChannelEnum.getName(this.getType()));
        order.setType("hand");
        order.setBankName("支付宝");
        order.setPic(picUrl);
        order.setZfbUrl(qrCode);
        order.setUserId(bank.getUserId());
        order.setUsername(bank.getUsername());

        ApiPayResultVo apiVo = new ApiPayResultVo();
        apiVo.setAmount(String.valueOf(order.getAmount()));
        apiVo.setOrderNo(order.getOrderNo());
        apiVo.setMerchantId(order.getMerchantId());
        TreeMap<String, String> map = SignMD5Util.objToMap(apiVo);
        String sign = SignMD5Util.getSign(map, "sign");
        String payUrl = configService.selectConfigByKey("payUrl");
        payUrl = payUrl + "?" + SignMD5Util.getStr(map, sign);
        apiVo.setUrl(payUrl);


        return apiVo;
    }


}
